// (C) Copyright 2002-2007 by Autodesk, Inc. 
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted, 
// provided that the above copyright notice appears in all copies and 
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting 
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. 
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC. 
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to 
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

//-----------------------------------------------------------------------------
//----- CDocReactor.cpp : Implementation of CDocReactor
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "CDocReactor.h"

//-----------------------------------------------------------------------------
// ACRX_CONS_DEFINE_MEMBERS(CDocReactor, AcApDocManagerReactor, 1)
//-----------------------------------------------------------------------------
CDocReactor::CDocReactor (const bool autoInitAndRelease) : AcApDocManagerReactor(), mbAutoInitAndRelease(autoInitAndRelease) {
	AcApDocumentIterator *iter = acDocManager->newAcApDocumentIterator();
	for (;!iter->done(); iter->step()) {
		if (reactors.find(iter->document()) == reactors.end())	{
			reactors[iter->document()] = new CEdSSgetFilter(iter->document());
		}
	}

	if ( autoInitAndRelease ) {
		if ( acDocManager )
			acDocManager->addReactor(this);
		else
			mbAutoInitAndRelease = false;
	}
}

//-----------------------------------------------------------------------------
CDocReactor::~CDocReactor () {
	Detach () ;
	for (ReactIter iter = reactors.begin(); iter != reactors.end(); iter++) {
		if (iter->second) delete iter->second; iter->second = NULL;
	}
	reactors.clear();
}

//-----------------------------------------------------------------------------
void CDocReactor::Attach () {
	Detach () ;
	if ( !mbAutoInitAndRelease ) {
		if ( acDocManager ) {
			acDocManager->addReactor(this);	mbAutoInitAndRelease = true ;
		}
	}
}

void CDocReactor::Detach () {
	if ( mbAutoInitAndRelease ) {
		if ( acDocManager ) {
			acDocManager->removeReactor(this); mbAutoInitAndRelease = false;
		}
	}
}

AcApDocManager *CDocReactor::Subject () const {
	return (acDocManager) ;
}

bool CDocReactor::IsAttached () const {
	return (mbAutoInitAndRelease) ;
}


// -----------------------------------------------------------------------------
void CDocReactor::documentCreated(AcApDocument * pDocCreating)
{
	reactors[pDocCreating] = new CEdSSgetFilter(pDocCreating);
	AcApDocManagerReactor::documentCreated(pDocCreating);
}

// -----------------------------------------------------------------------------
void CDocReactor::documentToBeDestroyed(AcApDocument * pDocToDestroy)
{
	ReactIter iter = reactors.find(pDocToDestroy);
	if (iter != reactors.end()) {
		delete iter->second; iter->second = NULL;
	}
	AcApDocManagerReactor::documentToBeDestroyed(pDocToDestroy);
}
